<template>
	<div class="hy-container">
		<HyPageHeader :title="`${title}信息录入`" :buttonData="buttonData">
			<HyCard title="单位信息">
				<div class="py-5 px-5">
					<unitInfo ref="unitInfoRef" :areaList="areaList" :basicInfoData="basicInfoData" :routerParams="routerParams"></unitInfo>
				</div>
			</HyCard>
			<div class="mt-5"></div>
			<div class="mt-5"></div>
			<HyCard title="设计单位信息">
				<div class="py-5 px-5">
					<designUnitInfo ref="designUnitInfoRef" :designUnitInfoData="designUnitInfoData" :routerParams="routerParams"></designUnitInfo>
				</div>
			</HyCard>
			<div class="mt-5"></div>
			<div class="mt-5"></div>
			<HyCard title="应用信息">
				<div class="py-5 px-5">
					<applyInfo ref="applyInfoRef" :applyInfoData="applyInfoData" :routerParams="routerParams"></applyInfo>
				</div>
			</HyCard>
		</HyPageHeader>
	</div>
</template>
<script setup lang="ts">
import HyCard from '/@/views/digitalList/detail/components/Card.vue';
import { useCounter } from './hooks/useCounter';
import HyPageHeader from '/@/components/HyPageHeader/index.vue';
import unitInfo from './components/unitInfo/index.vue';
import designUnitInfo from './components/designUnitInfo/index.vue';
import applyInfo from './components/applyInfo/index.vue';
import { sensitiveAppTreeAndSelfApi, sensitiveApplicationAddApi } from '/@/api/sensitiveData/informationEntry';
import { ElLoading } from 'element-plus';
import { removeEmptyStringsDeep, convertStringNumbers, findNameByName, isMultiDimensionalArray } from './utils/default';
const router = useRouter();
const { title, routerParams, basicInfoData, designUnitInfoData, applyInfoData } = useCounter();
const unitInfoRef = ref();
const designUnitInfoRef = ref();
const applyInfoRef = ref();
const setRequestParams = () => {
	const basicInfo = unitInfoRef.value.getData().formData;
	const designUnitInfo = designUnitInfoRef.value.getData().formData;
	const applyInfo = applyInfoRef.value.getData().formData;
	return { basicInfo, designUnitInfo, applyInfo };
};
onMounted(() => {
	nextTick(() => {
		getArea();
	});
});
//获取部门
const areaList: any = ref();
const getArea = () => {
	// let zhi = '1';
	// let data = { parentId: zhi };
	sensitiveAppTreeAndSelfApi().then((res) => {
		areaList.value = res;
	});
};
//提交
const handleSubmit = async (operationType: string) => {
	const checked = await Promise.all([
		unitInfoRef.value.unitInfocheck(),
		designUnitInfoRef.value.designUnitInfocheck(),
		applyInfoRef.value.applyInfocheck(),
	]);

	if (checked.every((item) => Boolean(item))) {
		const loading = ElLoading.service({
			lock: true,
			text: '提交中...',
		});

		const requestParams = JSON.parse(JSON.stringify(setRequestParams()));

		const leadDeptArr = isMultiDimensionalArray(requestParams.basicInfo.leadDeptId)
			? requestParams.basicInfo?.leadDeptId?.map((item: any) => {
					return item.at(-1);
			  })
			: requestParams.basicInfo.leadDeptId;

		const budgetUnitArr = isMultiDimensionalArray(requestParams.basicInfo.budgetUnitId)
			? requestParams.basicInfo?.budgetUnitId?.map((item: any) => {
					return item.at(-1);
			  })
			: requestParams.basicInfo.budgetUnitId;

		const responsibleDeptArr = isMultiDimensionalArray(requestParams.basicInfo.responsibleDeptId)
			? requestParams.basicInfo?.responsibleDeptId?.map((item: any) => {
					return item.at(-1);
			  })
			: requestParams.basicInfo.responsibleDeptId;

		requestParams.basicInfo.leadDeptId = leadDeptArr?.join(',');
		requestParams.basicInfo.leadDept = leadDeptArr
			.map((item: any) => {
				return findNameByName(areaList.value, item);
			})
			?.join(',');

		requestParams.basicInfo.responsibleDeptId = responsibleDeptArr?.join(',');
		requestParams.basicInfo.responsibleDept = responsibleDeptArr
			.map((item: any) => {
				return findNameByName(areaList.value, item);
			})
			?.join(',');

		requestParams.basicInfo.budgetUnitId = budgetUnitArr?.join(',');
		requestParams.basicInfo.budgetUnit = budgetUnitArr
			.map((item: any) => {
				return findNameByName(areaList.value, item);
			})
			?.join(',');

		requestParams.applyInfo.fundResource = requestParams.applyInfo?.fundResource?.length > 0 ? requestParams.applyInfo?.fundResource?.join(',') : '';
		requestParams.applyInfo.hierarchy = requestParams.applyInfo?.hierarchy?.length > 0 ? requestParams.applyInfo?.hierarchy?.join(',') : '';
		requestParams.applyInfo.mode = requestParams.applyInfo?.mode?.length > 0 ? requestParams.applyInfo?.mode?.join(',') : '';

		const obj = convertStringNumbers(
			removeEmptyStringsDeep({
				...requestParams.basicInfo,
				...requestParams.designUnitInfo,
				...requestParams.applyInfo,
				operationType,
			})
		);

		sensitiveApplicationAddApi(obj)
			.then((res) => {
				router.push('/sensitiveData/index');
			})
			.finally(() => {
				loading.close();
			});
	}
};

const goBack = () => {
	window.history.length > 1 ? router.go(-1) : router.push('/');
};
//按钮
const buttonData = computed(() => {
	return [
		{ name: '取消', click: () => goBack() },
		{ name: '确定', class: 'blue-button', click: () => handleSubmit('2') },
	];
});
</script>
